package housekeeping.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import housekeeping.pojo.Worker;
import housekeeping.pojo.WorkerApplication;
import housekeeping.service.WorkerApplicationService;
import housekeeping.service.WorkerService;
import housekeeping.util.ServerResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @version 1.0
 * @Author Du
 * @Date 2025/7/8 00:05
 * @注释
 */
@RestController
@CrossOrigin
@Tag(name="审批管理")
public class WorkerApplicationController {
    @Autowired
    private WorkerApplicationService workerApplicationService;

    @Autowired
    private WorkerService workerService;

    @GetMapping("/workerApplication/list")
    @Operation(summary = "显示所有审批条目")
    public ServerResult<List<WorkerApplication>> getWorkerApplications() {
        List<WorkerApplication> list = workerApplicationService.getWorkerApplications();
        return ServerResult.ok(list);
    }

    @PostMapping("/workerApplication/update")
    @Operation(summary = "更新审批条目")
    public ServerResult<Void> updateWorkerApplication(@RequestBody WorkerApplication workerApplication){
        try{
            workerApplicationService.updateWorkerApplication(workerApplication);
            if(workerApplication.getApplicationStatus().equals("已通过")){
//                private Integer id; // 家政人员ID
//                private String name; // 姓名
//                private String phone; // 联系电话
//                private String idCard; // 身份证号
//                private String image;
//                private String gender; // 性别
//                private Integer age; // 年龄
//                private String address; // 服务地址
//                private String serviceType; // 服务类型
//                private String workExperience; // 工作经验
//                @JsonFormat(pattern = "yyyy-MM-dd")
//                private LocalDate joinDate; // 上传简历日期
//                private String status; // 状态
                Worker worker = new Worker();
                worker.setId(workerApplication.getId());
                worker.setName(workerApplication.getName());
                worker.setPhone(workerApplication.getPhone());
                worker.setIdCard(workerApplication.getIdCard());
                worker.setImage(workerApplication.getImage());
                worker.setGender(workerApplication.getGender());
                worker.setAge(workerApplication.getAge());
                worker.setAddress(workerApplication.getAddress());
                worker.setServiceType(workerApplication.getServiceType());
                worker.setWorkExperience(workerApplication.getWorkExperience());
                worker.setJoinDate(workerApplication.getJoinDate());
                worker.setStatus("在职");
                workerService.updateWorker(worker);
            }

        }
        catch (RuntimeException e){
            return ServerResult.error(500, e.getMessage());
        }
        return ServerResult.ok();
    }

    @PostMapping("/workerApplication/insert")
    @Operation(summary = "插入新的审批条目")
    public ServerResult<Void> insertWorkerApplication(@RequestBody Worker worker){
        WorkerApplication workerApplication = new WorkerApplication();
        //worker_id, name, phone, id_card, gender, age,
        //service_type, work_experience, join_date, application_status,
        // review_time, reviewer_id
        workerApplication.setWorkerId(worker.getId());
        workerApplication.setName(worker.getName());
        workerApplication.setPhone(worker.getPhone());
        workerApplication.setIdCard(worker.getIdCard());
        workerApplication.setGender(worker.getGender());
        workerApplication.setAge(worker.getAge());
        workerApplication.setImage(worker.getImage());
        workerApplication.setAddress(worker.getAddress());
        workerApplication.setWorkerStatus(worker.getStatus());
        workerApplication.setServiceType(worker.getServiceType());
        workerApplication.setWorkExperience(worker.getWorkExperience());
        workerApplication.setJoinDate(worker.getJoinDate());
        workerApplication.setApplicationStatus("待审批");
        workerApplicationService.insertWorkerApplication(workerApplication);
        return ServerResult.ok();
    }
}
